﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using MethodWorx.CMS;
using System.Web.Routing;

namespace System
{
    public class FormButtons
    {
        private HtmlHelper helper;
        private string html;

        

        public FormButtons(HtmlHelper helper)
        {
            this.helper = helper;
        }

        public void Submit(string name, string text)
        {
            html += "<button id=\"" + name + "\" name=\"submit\" value=\"" + name + "\">" + text + "</button>";
        }

        public void LinkButton(string name, string text, string url)
        {
            html += "<button id=\"" + name + "\" name=\"button\" value=\"" + name + "\" onclick=\"window.location.href = '" + url + "'\">" + text + "</button>";
        }

        public string Render()
        {
            return html;
        }
    }
    public static class HtmlExtensions
    {
        public static string PluginAction(this HtmlHelper helper, string actionName)
        {
            var context = new HttpContextWrapper(HttpContext.Current);
            RouteValueDictionary duplicated = new RouteValueDictionary(helper.ViewContext.RouteData.Values);
            duplicated["action"] = actionName;
            return UrlHelper.GenerateContentUrl("~/" + helper.RouteCollection["Plugins"].GetVirtualPath(helper.ViewContext.RequestContext, duplicated).VirtualPath, context);
        }

        public static string GlobalPluginAction(this HtmlHelper helper, string actionName)
        {
            var context = new HttpContextWrapper(HttpContext.Current);
            RouteValueDictionary duplicated = new RouteValueDictionary(helper.ViewContext.RouteData.Values);
            duplicated["action"] = actionName;
            return
                UrlHelper.GenerateContentUrl(
                    "~/" +
                    helper.RouteCollection["GlobalPlugins"].GetVirtualPath(helper.ViewContext.RequestContext, duplicated)
                        .VirtualPath, context);
        }

        private static string BeginFormItem(string label)
        {
            string html = "";
            html += "<div class=\"formItem\">";
            html += "<label>" + label + "</label>";

            return html;
        }

        private static string EndFormItem(this HtmlHelper helper, string name)
        {
            string html = "";
            html += "<br/>";
            html += helper.ValidationMessage(name);
            html += "</div>";

            return html;
        }
        #region textbox
        public static string FormTextBox(this HtmlHelper helper, string label, string name)
        {
            return FormTextBox(helper, label, name, null);
        }

        public static string FormTextBox(this HtmlHelper helper, string label, string name, object value)
        {
            return FormTextBox(helper, label, name, value, null);
        }



        public static string FormTextBox(this HtmlHelper helper, string label, string name, object value, object attributes)
        {
            string html = "";

            html += BeginFormItem(label);
            html += helper.TextBox(name, value, attributes);
            html += EndFormItem(helper, name);
  

            return html;
        }
        #endregion
        #region textarea
        public static string FormTextArea(this HtmlHelper helper, string label, string name)
        {
            return FormTextArea(helper, label, name, null);
        }

        public static string FormTextArea(this HtmlHelper helper, string label, string name, object value)
        {
            return FormTextArea(helper, label, name, value, null);
        }

        public static string FormTextArea(this HtmlHelper helper, string label, string name, object value, object attributes)
        {
            string html = "";

            html += BeginFormItem(label);
            html += helper.TextArea(name, (string)(value ?? ""), attributes);
            html += EndFormItem(helper, name);


            return html;
        }
        #endregion

        public static string Script(this HtmlHelper helper, string name, string script)
        {
            string output = "";
            if (!HttpContext.Current.Items.Contains("Script:" + name))
            {
                output = "<script language=\"javascript\">";
                output += script;
                output += "</script>";
                HttpContext.Current.Items.Add("Script:" + name, "");
            }

            return output;
        }

        public static string ScriptFile(this HtmlHelper helper, string name, string script)
        {
            string output = "";
            if (!HttpContext.Current.Items.Contains("Script:" + name))
            {
                output = "<script language=\"javascript\" src=\"" + UrlHelper.GenerateContentUrl(script, helper.ViewContext.HttpContext) + "\">";
                output += "</script>";
                HttpContext.Current.Items.Add("Script:" + name, "");
            }

            return output;
        }


        public static string CssFile(this HtmlHelper helper, string name, string css)
        {
            string output = "";
            if (!HttpContext.Current.Items.Contains("Css:" + name))
            {
                output = "<link href=\"" + UrlHelper.GenerateContentUrl(css, helper.ViewContext.HttpContext) + "\" type=\"text/css\" rel=\"stylesheet\" />";
                HttpContext.Current.Items.Add("Css:" + name, "");
            }

            return output;
        }

        public static string FormCheckBox(this HtmlHelper helper, string label, string name)
        {
            return FormCheckBox(helper, label, name, false);
        }

        public static string FormCheckBox(this HtmlHelper helper, string label, string name, bool isChecked)
        {
            string html = "";

            html += BeginFormItem(label);
            html += helper.CheckBox(name, isChecked);
            html += EndFormItem(helper, name);


            return html;
        }

        public static string FormButtons(this HtmlHelper helper, Action<FormButtons> del)
        {
            string html = "";
            var buttons = new FormButtons(helper);
            
            html += "<div class=\"formItem\">";
            del(buttons);
            html += buttons.Render();
            html += "</div>";

            return html;
        }

        public static string FormMultiSelect(this HtmlHelper helper, string label, string name, string [] items, object val)
        {
            string html = "";
            html += "<div class=\"formItem\">";
            html += "<label>" + label + "</label>";
            html += "<div style=\"clear:both\"></div>";
            html += "<div style=\"height:100px; background-color:white; border:1px solid silver;overflow:auto;\">";
            foreach (string item in items)
            {
                html += "<div>" + helper.CheckBox(name + "." + item.Replace(" ", ""), (val.GetType().GetProperty(item.Replace(" ", "")).GetValue(val, null).Equals(true))) + " " + item + "</div>";
            }
            html += "</div>";
            html += "</div>";

            return html;
        }

        public static string FormDropDown(this HtmlHelper helper, string label, string name, SelectList selectList)
        {
            string html = "";
            html += BeginFormItem(label);
            html += helper.DropDownList(name, selectList).ToHtmlString();
            html += EndFormItem(helper, name);

            return html;
        }

        public static string FormDropDown(this HtmlHelper helper, string label, string name, SelectList selectList, string optionLabel)
        {
            string html = "";
            html += BeginFormItem(label);
            html += helper.DropDownList(name, selectList, optionLabel).ToHtmlString();
            html += EndFormItem(helper, name);

            return html;
        }

        public static string FormEnumDropDown(this HtmlHelper helper, string label, string name, Type enumType, object val)
        {
            string html = "";
            Dictionary<object, string> items = new Dictionary<object,string>();
            foreach (var fi in enumType.GetFields())
            {
                if (!fi.IsSpecialName)
                {
                    items.Add(Enum.Parse(enumType, fi.Name), fi.Name);
                }
            }

            html += BeginFormItem(label);
            html += helper.DropDownList(name, new SelectList(items, "Key", "Value", val)).ToHtmlString();
            html += EndFormItem(helper, name);

            return html;
        }
    }
}